using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace TestCases.CompareAssert.Matchers
{
    public class StringMatcher: ObjectMatcher<string>
    {
        private readonly HashSet<Regex> _expressions = new HashSet<Regex>(RegexEqualityComparer.Instance);

        public StringMatcher()
        {            
        }
        
        public StringMatcher(string value) : base(value)
        {            
        }

        public StringMatcher(IEnumerable<string> values) : base(values)
        {            
        }
        
        public StringMatcher(Predicate<string> predicate) : base(predicate)
        {            
        }

        public StringMatcher(Regex expression)
        {
            _expressions.Add(expression);
        }


        public void AddRegex(Regex regex)
        {
            _expressions.Add(regex);
        }

        public override void Reset()
        {
            base.Reset();
            _expressions.Clear();
        }

        public override bool IsMatch(string v)
        {
            if (base.IsMatch(v))
            {
                return true;
            }
            
            foreach (var expression in _expressions)
            {
                if (expression.IsMatch(v))
                {
                    return true;
                }
            }            
            return false;
        }

        public bool Equals(StringMatcher other)
        {
            return base.Equals(other) && _expressions.SetEquals(other._expressions);
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as StringMatcher);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                {
                    return (base.GetHashCode()*397) ^ _expressions.GetHashCode();
                }
            }
        }
    }
}